package com.springboot.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.mall.common.BaseContext;
import com.springboot.mall.common.Result;
import com.springboot.mall.entity.OrderSelect;
import com.springboot.mall.entity.Orders;
import com.springboot.mall.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/submit")
    public Result<String> submit(@RequestBody Orders orders){
        orderService.submit(orders);
        return Result.success("下单成功");
    }

    /**
     * 订单分页查询
     * @param o
     * @return
     */
    @GetMapping("/page")
    public Result<Page<Orders>> page(OrderSelect o){
        Page<Orders> pageInfo = new Page<>(o.getPage(),o.getPageSize());
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(o.getNumber() != null,Orders::getNumber,o.getNumber());
        queryWrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(pageInfo, queryWrapper);
        return Result.success(pageInfo);
    }

    /**
     * 订单派送
     * @param orders
     * @return
     */
    @PutMapping
    public Result<String> send(@RequestBody Orders orders){
        UpdateWrapper<Orders> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",orders.getId());
        wrapper.set("status",orders.getStatus());
        orderService.update(wrapper);
        return Result.success("派送成功");
    }

    /**
     * 用户订单分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public Result<Page<Orders>> userOrders(int page,int pageSize){
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByDesc(Orders::getOrderTime);
        Page<Orders> list = orderService.page(pageInfo, queryWrapper);
        return Result.success(list);
    }
}
